﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UBlockly.UI;
using UBlockly.Compotent;
using UBlockly.Graphic;

namespace UBlockly.Sys
{

    internal class BlockSystem 
    {
        private UBlockEntry blockEntry;
        private Transform rootTransformForNode;
        private List<BlockObject> theWholeBlocks = new List<BlockObject>();
        internal List<SlotTypeBlock> slotTypeBlockList = new List<SlotTypeBlock>();
        internal List<PackerObject> packerList = new List<PackerObject>();

        internal BlockSystem(UBlockEntry entry)
        {
            blockEntry = entry;
            rootTransformForNode = entry.BlockPhysic.NodeRootTransform;
        }

        internal void AddBlock(BlockObject block)
        {
            IBranchNode bnode = block.Node.Value as IBranchNode;
            if (bnode == null) //非分支node
            {
                if (block.BlockType == BlockType.SlotType)
                    slotTypeBlockList.Add(block as SlotTypeBlock);
                if(block.GetPackerObject() != null)
                    packerList.Add(block.GetPackerObject());
            }
            else
            {
                for (int i = 0; i < bnode.BranchCount; i++)
                {
                    AddBlock(bnode.GetBranchRoot(i).Block);
                }
            }
        }
        internal void RemoveBlock(BlockObject block)
        {
            IBranchNode bnode = block.Node.Value as IBranchNode;
            if (bnode == null) //非分支node
            {
                if (block.BlockType == BlockType.SlotType)
                    slotTypeBlockList.Remove(block as SlotTypeBlock);
                if (block.GetPackerObject() != null)
                    packerList.Remove(block.GetPackerObject());
            }
            else
            {
                for (int i = 0; i < bnode.BranchCount; i++)
                {
                    RemoveBlock(bnode.GetBranchRoot(i).Block);
                }
            }
        }
        internal void DoPack(PackTypeBlock target, PackerObject bePacked, int index)
        {
            var replacceNode = Pack(target, bePacked, index);
            if (replacceNode != null)
            {
                var blockTrans = replacceNode.Block.transform;
                var pos = blockTrans.position;
                replacceNode.Block.UnAttach(rootTransformForNode);
                blockTrans.position = pos;
                blockTrans.localPosition += new Vector3(100, 100);
            }
        }
        internal void DoSlot(BlockObject target, BlockObject beSloted)
        {
            Slot(target, beSloted);
        }
        internal static void Slot(BlockObject target, BlockObject beSloted)
        {
            //不用去区分分支
            ULinkedList<Node> list = target.Node.List as ULinkedList<Node>;
            if (list == null)
            {
                list = new ULinkedList<Node>();
                list.MuteNotify();
                list.AddFirst(target.Node);
            }
            beSloted.Node.Insert(list);
        }
        internal static Node Pack(PackTypeBlock target, PackerObject bePacked, int index)
        {
            Debug.Log($"执行物理嵌套关系{target == null} - {bePacked == null} - {index}");
            Node replaceNode = null;
            if (bePacked.GetParamObject(index).IsPacked())
            {
                replaceNode = bePacked.UnPackNode(index);
            }

            bePacked.PackNode(target.Node.Value,index);                //设置逻辑嵌套关系
            return replaceNode;
        }
    }
}
